package com.kevms.phonewars;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

/** 
* @class JoinMatchList
* 
* @brief ListActivity used to display all available matches.
* 
* This ListActivity displays information about all of the matches
* available for the user to select from and join in a List View.
*/ 
public class JoinMatchList extends ListActivity {

	private WebService mWebService;
	private ArrayList<HashMap<String, String>> mList;
	private ListView mListView;
	private SimpleAdapter mAdapter = null;
	private Context mContext;
	
	private SQLiteDatabase db;
	private MySQLiteHelper dbHelper;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.join_match_list);
		mContext = this;
		
		dbHelper = MySQLiteHelper.getInstance(getApplicationContext());
		db = dbHelper.getWritableDatabase();
		LinearLayout l = (LinearLayout)findViewById(R.id.topLayout);
    	l.setBackgroundColor(dbHelper.getBgColor(db));
		
		mWebService = WebService.getInstance(this);
		mList = new ArrayList<HashMap<String, String>>();
		
		refreshList();
		
		mListView = getListView();
		mListView.setTextFilterEnabled(true);
		
		mListView.setOnItemClickListener(new OnItemClickListener() {
        	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        		
        		LinearLayout layout = (LinearLayout)view;
        		TextView tvMatchID = (TextView)layout.getChildAt(0);
        		
        		Intent intent = new Intent(getApplicationContext(), JoinMatchEntry.class);
        		intent.putExtra("match_id", tvMatchID.getText().toString());
        		startActivityForResult(intent,0);
        	}
        });
		
		Button btnRefresh = (Button)findViewById(R.id.btnRefresh);
		btnRefresh.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				refreshList();
			}
		});
	}
	
	private void refreshList() {
		JSONArray jArray = null;
		
		try {
			jArray = mWebService.getMatchList();
			if(jArray != null) {
				mList.clear();
				for(int i=0;i<jArray.length();i++) {
					HashMap<String, String> map = new HashMap<String, String>();
					JSONObject json_data = jArray.getJSONObject(i);
					map.put("id", json_data.getString("id"));
					map.put("name",json_data.getString("name"));
					map.put("host", json_data.getString("host"));
					map.put("count", json_data.getString("count"));
					map.put("type", json_data.getString("type"));
					mList.add(map);
				}
			}
		} catch (JSONException e) {
			mList.clear();
			e.printStackTrace();
		} catch (Exception e) {
			mList.clear();
			e.printStackTrace();
		}
		
		mAdapter = new SimpleAdapter(
				mContext,
				mList, // cursor to bind to
				R.layout.join_match_list_item, // row template to use
				new String[] {"id", "name", "host", "count", "type" }, // array of map keys to bind to
				new int[] {R.id.tvMatchID, R.id.tvMatchName, R.id.tvMatchHost, R.id.tvMatchPlayerCount, R.id.tvMatchType}); // parallel array of which template objects to bind to those columns
		
		setListAdapter(mAdapter);
		mAdapter.notifyDataSetChanged();
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		refreshList();
    }
}